<?php 
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: list.phtml 4989 2010-08-26 04:14:28Z huuphuoc $
 * @since		2.0.0
 */
?>
<?php 
$this->headTitle($this->translator('file_list_page_title'));

$this->headLink()
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.core.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.resizable.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.dialog.css')
	->appendStylesheet($this->APP_STATIC_SERVER . '/js/jquery.ui/themes/base/ui.theme.css');

$this->headScript()
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.core.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.draggable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.resizable.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.ui/ui.dialog.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.editinplace/jquery.editinplace.packed.js')
	->appendFile($this->APP_STATIC_SERVER . '/js/jquery.validate/jquery.validate.min.js');
?>

<div class="grid_12 t_a_ui_breadcrumb">
	<span><?php echo $this->translator('file_list_breadcrumb'); ?></span>	
</div>

<div class="grid_12 t_a_bottom">
	<div class="grid_8 alpha"><?php echo $this->translator('file_list_guide'); ?></div>
	<div class="grid_4 omega">
		<div class="t_a_right">
			<a style="margin: 0 10px" class="t_a_ui_button_link" href="<?php echo $this->url(array(), 'multimedia_photo_upload'); ?>"><span><?php echo $this->translator('photo_upload_breadcrumb'); ?></span></a> <a class="t_a_ui_button_link" href="<?php echo $this->url(array(), 'multimedia_file_add'); ?>"><span><?php echo $this->translator('file_add_breadcrumb'); ?></span></a>
		</div>
	</div>	
</div>

<!-- Search form -->
<form method="post" action="<?php echo $this->url(array(), 'multimedia_file_list'); ?>" id="searchFileForm">
<div class="grid_12 t_a_bottom">
	<div class="prefix_1 suffix_1">
		<div class="clearfix t_a_bottom">
			<label><?php echo $this->translator('file_list_search_keyword'); ?>:</label>
			<input type="text" style="width: 300px" name="keyword" value="<?php if (isset($this->exp['keyword'])) : ?><?php echo $this->exp['keyword']; ?><?php endif; ?>" />
		</div>
		
		<hr />
		
		<div class="clearfix t_a_bottom">
			<label><?php echo $this->translator('file_list_search_id'); ?>:</label>
			<input type="text" style="width: 100px" name="fileId" id="fileId" value="<?php if (isset($this->exp['file_id'])) : ?><?php echo $this->exp['file_id']; ?><?php endif; ?>" />
		</div>
		
		<hr />
		
		<div class="clearfix">
			<label>&nbsp;</label>
			<input type="checkbox" name="findPhotos"<?php if (isset($this->exp['photo'])) : ?> checked="checked"<?php endif; ?> value="1" /> <?php echo $this->translator('file_list_search_photos'); ?>
		</div>
		
		<div class="clearfix">
			<label>&nbsp;</label>
			<input type="checkbox" name="findClips"<?php if (isset($this->exp['clip'])) : ?> checked="checked"<?php endif; ?> value="1" /> <?php echo $this->translator('file_list_search_clips'); ?>			
		</div>
		
		<hr />
		
		<div class="clearfix">
			<label>&nbsp;</label>
			<input type="checkbox" name="findMyFiles"<?php if (isset($this->exp['created_user'])) : ?> checked="checked"<?php endif; ?> value="1" /> <?php echo $this->translator('file_list_search_my_files'); ?>
		</div>
		
		<hr />
		
		<div class="clearfix t_a_bottom">
			<label>&nbsp;</label>
			<button type="submit"><span><?php echo $this->translator('file_list_search_button'); ?></span></button>
		</div>
    </div>
</div>
</form>

<!-- List of files -->
<?php if ($this->numFiles == 0) : ?>
	<div class="grid_12 t_a_bottom"><?php echo $this->translator('file_list_not_found'); ?></div>	
<?php else : ?>	
	<?php foreach ($this->files as $index => $file) : ?>
		<?php if ($index % 3 == 0) : ?><ul class="grid_12 t_a_bottom"><?php endif; ?>
		
		<li id="fileRow_<?php echo $file->file_id; ?>" style="padding: 10px 30px; float: left">
			<div class="clearfix t_a_bottom "><img class="t_a_image" src="<?php echo $file->image_small; ?>" /></div>
			
			<div class="clearfix t_a_bottom t_a_ui_helper_editable" style="background: #E8E8E8; border: 1px solid #222; width: 160px; max-height: 200px; overflow: auto" id="title_<?php echo $file->file_id; ?>"><?php echo $file->title; ?></div>
			<div class="clearfix t_a_bottom t_a_ui_helper_editable" style="background: #E8E8E8; border: 1px solid #222; width: 160px; max-height: 200px; overflow: auto" id="description_<?php echo $file->file_id; ?>"><?php echo $file->description; ?></div>
			
			<div class="t_a_bottom t_a_ui_helper_action">
				<?php if ($this->allow('edit')) : ?>
				<a href="<?php echo $this->url(array('file_id' => $file->file_id), 'multimedia_file_edit'); ?>"><?php echo $this->translator('file_list_action_edit'); ?></a> |
				<?php endif; ?>
				
				<?php if ($this->allow('delete')) : ?> 
				<a class="deleteAction" rel="<?php echo $file->file_id; ?>" href="javascript: void(0);"><?php echo $this->translator('file_list_action_delete'); ?></a> |
				<?php endif; ?>
				
				<?php if ($this->allow('activate')) : ?>
				 <a class="activateAction" rel="<?php echo $file->file_id ?>_<?php echo (int)$file->is_active; ?>_<?php echo $file->slug; ?>" href="javascript: void(0);"><?php if ($file->is_active) : ?><?php echo $this->translator('file_list_action_deactivate'); ?><?php else : ?><?php echo $this->translator('file_list_action_activate'); ?><?php endif; ?></a>
				<?php endif; ?>
			</div>
		</li>
		
		<?php if ($index % 3 == 2 || $index == (count($this->files) - 1)) : ?></ul><?php endif; ?>
	<?php endforeach; ?>
	
	<div class="grid_12 t_a_bottom t_a_ui_paginator">
		<?php echo $this->paginator()->slide($this->paginator, $this->paginatorOptions); ?>
	</div>
<?php endif; ?>

<div id="messageDialog"></div>

<div id="confirmDialog"></div>

<script type="text/javascript">
$(document).ready(function() {
	<?php if ($this->allow('edit')) : ?>
	$('.t_a_ui_helper_editable').editInPlace({
		url: "<?php echo $this->url(array('file_id' => 0), 'multimedia_file_edit'); ?>",
		bg_out: '#f3f3f3',
		field_type: 'textarea',
		textarea_cols: 17,
		textarea_rows: 2,
		saving_text: '<?php echo $this->translator('file_list_saving'); ?>',	
		callback: function(original_element, html, original) {
			$.ajax({
				url: "<?php echo $this->url(array('file_id' => 0), 'multimedia_file_edit'); ?>",
				type: 'POST',
				data: { keySection: original_element, value: html },
				success: function(response) {
					$('#messageDialog').html('<?php echo $this->translator('file_list_update_success'); ?>')
					.dialog({
						title: '<?php echo $this->translator('common_dialog_message'); ?>',
						dialogClass: 't_a_ui_dialog',
						modal: true,
				    	show: { effect: 'fade' },
						open: function(event, ui) {
							setTimeout(function() {
								$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
							}, 3000);
						}
					});
				}
			});
			return(html);
	    }	
	});
	<?php endif; ?>
	
	<?php if ($this->allow('delete')) : ?>
	$('a.deleteAction').click(function() {
		var self = this;
		$('#confirmDialog').html('<?php echo addslashes($this->translator('file_list_action_delete_confirm')); ?>')
			.dialog({
				title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
				dialogClass: 't_a_ui_dialog',
				modal: true,
				closeOnEscape: false,
				resizable: false,
				open: function(event, ui) {
					$('.ui-dialog-titlebar-close').hide();
					$(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
				},
				buttons: {
					'<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
						$(this).dialog('destroy');
					},
					'<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
						$(this).dialog('destroy');
						$(self).html('').addClass('t_a_ui_helper_loading');
						var id = $(self).attr('rel');
						$.ajaxq('multimedia_file', {
							type: 'POST',
							url: "<?php echo $this->url(array(), 'multimedia_file_delete'); ?>",
							data: { id: id },
							success: function(response) {
								if ('RESULT_OK' == response) {
									$('#fileRow_' + id).fadeOut('slow');
								}
							}
						});
					}
				}
			});
	});
	<?php endif; ?>

	<?php if ($this->allow('activate')) : ?>
	$('a.activateAction').click(function() {
		activateFile(this);
	});
	<?php endif; ?>

	<?php if ($this->allow('activate')) : ?>
	function activateFile(linkObj) {
		var self = linkObj;
		var arr = $(self).attr('rel').split('_');
		$(self).html('').addClass('t_a_ui_helper_loading');
		$.ajaxq('multimedia_set', {
			url: "<?php echo $this->url(array(), 'multimedia_file_activate'); ?>",
			type: 'POST',
			data: { id: arr[0], status: arr[1] },
			success: function(response) {
				$(self).attr('rel', arr[0] + '_' + response + '_' + arr[2]);
				if (response == 1) {
					$(self).html("<?php echo $this->translator('file_list_action_deactivate'); ?>");
				} else {
					$(self).html("<?php echo $this->translator('file_list_action_activate'); ?>");
				}
				$(self).removeClass('t_a_ui_helper_loading').fadeIn('normal');

				$('#messageDialog').html((response == 1) ? sprintf('<?php echo addslashes($this->translator('file_list_activate_success')); ?>', arr[2]) : sprintf('<?php echo addslashes($this->translator('file_list_deactivate_success')); ?>', arr[2]))
					.dialog({
						title: '<?php echo addslashes($this->translator('common_dialog_message')); ?>',
						dialogClass: 't_a_ui_dialog',
						modal: true,
				    	show: { effect: 'fade' },
						open: function(event, ui) {
							setTimeout(function() {
								$('#' + event.target.id).dialog('option', 'hide', 'fade').dialog('destroy');
							}, 3000);
						}
					});
			}
		});
	};
	<?php endif; ?>

	/**
	 * Validate search form
	 */
	$('#searchFileForm').validate({
		rules: {		
			fileId: {
				number: true
			}
	    },
	    messages: {
	    	fileId: {
	        	number: "<?php echo $this->translator('file_list_id_invalid'); ?>"
			}
		}
	});
});
</script>
